package com.itheima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.admin.dto.AdSensitivePageRequestDto;
import com.itheima.admin.mapper.AdSensitiveMapper;
import com.itheima.admin.pojo.AdSensitive;
import com.itheima.admin.service.AdSensitiveService;
import com.itheima.common.vo.PageResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * @version 1.0
 * @description 频道管理业务实现
 * @package com.itheima.admin.service.impl
 */
@Service
@Slf4j
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    /**
     * 频道分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageResultVo<AdSensitive> findPage(AdSensitivePageRequestDto dto) {
        //1. 分页参数
        IPage<AdSensitive> pageSetting = new Page<>(dto.getPage(), dto.getSize());
        //2. 条件构建
        LambdaQueryWrapper<AdSensitive> queryWrapper = Wrappers.<AdSensitive>lambdaQuery();
        // 如果名称不为空，模糊查询. 当p1为true，这个条件才会生效
        queryWrapper.like(StringUtils.isNotEmpty(dto.getName()), AdSensitive::getSensitives, dto.getName());
        //3. 查询
        page(pageSetting, queryWrapper);
        //System.out.println("============pageSetting==pageInfo=>" + (pageSetting==pageInfo));
        //4. 组装结果返回
        return PageResultVo.pageResult(dto.getPage(),dto.getSize(),pageSetting.getTotal(),pageSetting.getRecords());
    }

    /**
     * 添加频道
     *
     * @param adSensitive
     */
    @Override
    public void add(AdSensitive adSensitive) {
        //1. 补充数据字段
        adSensitive.setCreatedTime(LocalDateTime.now());
        //2. 执行添加
        save(adSensitive);
    }
}
